package data.nanjing.expo.ctrls;

import data.nanjing.expo.common.Result;
import data.nanjing.expo.models.domain.ArrayDto;
import data.nanjing.expo.models.domain.NotifyService;
import data.nanjing.expo.models.domain.User;
import data.nanjing.expo.services.NotifyServiceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zjp on 2018/7/3.
 * QQ：34948062
 * github: https://www.github.com/zjp1029
 * web： http://www.zjp1029.cn
 * 消息通知Controller
 */
@RestController
@RequestMapping("/api/notify")
public class NotifyCtrl extends BaseCtrl {

    @Autowired
    private NotifyServiceService notifyServiceService;

    /**
     * 获取通知消息
     * @param status
     * @param request
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{status}")
    public Result getNotifyList(@PathVariable("status") String status, HttpServletRequest request,
                                @RequestParam(name = "pageNumber", defaultValue = "1") Integer pageNumber,
                                @RequestParam(name = "pageSize", defaultValue = "15") Integer pageSize) {
        try {
            User user = this.getUserFromSession(request);
            if (user == null) {
                return send(-2, "请登录");
            }
            if (status == null || (!("all").equals(status) && !"notRead".equals(status))) {
                status = "all";
            }
            Map<String, Object> map = new HashMap();
            int total = notifyServiceService.findNotifyCountByUserId(user.getId(), status);
            List<NotifyService> list = new ArrayList<>();
            if (total > 0) {
                list = notifyServiceService.findNotifyByUserId(user.getId(), status, pageNumber, pageSize);
            }
            map.put("total", total);
            map.put("data", list);
            return send(map, 1, "获取数据成功");
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "获取数据失败");
        }
    }

    /**
     * 设置消息为已读
     * @param notifyId
     * @param request
     * @return
     */
    @PostMapping("/read")
    public Result readNotify(@RequestParam("notifyId") Integer notifyId, HttpServletRequest request) {
        try {
            User user = this.getUserFromSession(request);
            if (user == null) {
                return send(-2, "请登录");
            }
            notifyServiceService.updateNotifyStatusByIdAndUserId(1, user.getId(), notifyId);
            return send(1, "消息已读");
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "读消息失败");
        }
    }

    /**
     * 删除消息通知
     * 假删除
     * @param notifyId
     * @param request
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteNotify(@RequestParam("notifyId") List<Integer> notifyId, HttpServletRequest request) {
        return this.send(notifyId);
        //try {
        //    User user = this.getUserFromSession(request);
        //    if (user == null) {
        //        return send(-2, "请登录");
        //    }
        //    for(Integer id : notifyId) {
        //        notifyServiceService.updateNotifyStatusByIdAndUserId(-1, user.getId(), id);
        //    }
        //    return send(1, "删除成功");
        //}catch (Exception e) {
        //    e.printStackTrace();
        //    return send(-1, "删除失败");
        //}
    }

    /**
     * 获取未读消息数量
     * @param request
     * @return
     */
    @GetMapping("/count")
    public Result getNotifyCount(HttpServletRequest request) {
        User user = this.getUserFromSession(request);
        if (user == null) {
            return send(-2, "请登录");
        }else {
            Integer count = notifyServiceService.selectNotifyCountByStatus(user.getId(), 0);
            return send( count, 1,"未读数量获取成功");
        }
    }


}
